<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .container {
            width: 400px;
            margin: 0 auto;
            height: 200px;
            overflow: scroll;
        }

        .section {
            border: 1px solid #ccc;
            margin-bottom: 20px;
        }

        .sticky {
            position: sticky;  /* 粘性定位 */
            top: 0;            /* 距离顶部 0px */
            background: white; /* 避免覆盖时背景透明 */
            padding: 5px 10px;
            border-bottom: 1px solid #999;
            z-index: 10;       /* 确保在内容之上 */
        }

    </style>
</head>
<body>
<div class="container">
    <div class="section">
        <h2 class="sticky">标题 1</h2>
        <p>内容内容内容内容内容...</p>
        <p>内容内容内容...</p>
    </div>

    <div class="section">
        <h2 class="sticky">标题 2</h2>
        <p>内容内容内容...</p>
    </div>

    <div class="section">
        <h2 class="sticky">标题 3</h2>
        <p>内容内容内容...</p>
    </div>
</div>

</body>
</html>